#include "key.h"
#include "usart.h"
#include "delay.h"
#include "buzzer.h"
#include "led.h"

extern uint8_t flag;
int cr,key_flag=0;
uint8_t key_len;
extern key_6byte buf_key;
extern uint8_t admin_flag;
uint8_t change_admin_uid_flag = 0;
uint8_t fk_flag = 0;
uint8_t wg_flag = 0;
uint8_t read_uid_flag[3];
uint8_t key_buffer[128];
uint8_t wg_t = 0;
char *s;
/**
 * @brief key_com 获取的密钥数据
 * @param  admin_key: 管理员密钥
 * @param  user_key:  用户密钥
  * @param  k_buf:  存放数据的结构体
 * @retval 1头数据
 */
void key_com(key_6byte *admin_key,key_6byte *user_key,uint8_t *k_buf)
{
	//判断数据长度是否超过缓冲数组
	if(key_len>=128)
	{
		key_flag = 0;
		key_len = 0;
	}
	//判断哪个按键按下
	switch (flag)
	{
		case 1  : { flag = 0;cr = 1; 	printf("1"); break;  }
		case 2  : { flag = 0;cr = 2; 	printf("2"); break;  }
		case 3  : { flag = 0;cr = 3; 	printf("3"); break;  }
		case 4  : { flag = 0;cr = 4; 	printf("4"); break;  }
		case 5  : { flag = 0;cr = 5; 	printf("5"); break;  }
		case 6  : { flag = 0;cr = 6; 	printf("6"); break;  }
		case 7  : { flag = 0;cr = 7; 	printf("7"); break;  }
		case 8  : { flag = 0;cr = 8; 	printf("8"); break;  }
		case 9  : { flag = 0;cr = 9; 	printf("9"); break;  }
		case 12 : { flag = 0;cr = '#'; 	printf("#"); break;  }
		case 11 : 
		{ 
			flag = 0;
			cr = 0; 	
			printf("0");
			if(key_flag == 0)
			{
				wg_t++;//读头标志
			}
			if(wg_t >=3)
			{
				wg_flag = 0;
				wg_t = 0;
			}				
			break;  
		}
		case 10 : 
		{ 
			flag = 0;cr = '*';	
			key_len = 0;
			admin_flag = 0;//增卡减卡标志清零
			change_admin_uid_flag = 0;//修改管理员卡标志清零
			memset(key_buffer,0,128);//清零输入按键数据
			printf("*"); 
			break;  
		}
		default:
		break;
	}
	
	//读头标志清零
	if(cr != 0 && wg_t <3)
	{
		wg_t = 0;
	}
	
	LED_COM();//清零定时器14的计数器
	
	switch(key_flag)
	{
		//待机模式，*切换成编程模式
		case 0:
		{
			if(cr == '*')
			{	
				key_len = 0;
				key_flag = 1;
			}
			break;
		}
		
		//编程模式，*退出到待机模式。#确认
		case 1:
		{
			if(cr == '#')
			{
				key_flag = 2;
			}
			else if(cr == '*')
			{
				key_flag = 0;
			}
			else
			{
				key_buffer[key_len++] = cr;
			}
			break;
		}
		//选择状态模式
		case 3:
		{
			command_select();
			break;
		}
		case 4:key_b(k_buf,0,admin_key,user_key);break;//修改管理员密钥
		case 5:key_b(k_buf,1,admin_key,user_key);break;//修改用户密钥
		case 6:Format_uf(k_buf,0,admin_key);break;//删除所有卡
		case 7:Format_uf(k_buf,1,admin_key);break;//格式化
		case 8:read_uidstatus();break;//读头模式
	}
	//判断输入的6位密钥是否为管理员密钥
	if(key_flag == 2)
	{
		if(key_len<6)//密码错误
		{
			#if DEBUG
            printf("key_len_error\r\n");
			Buzzer_RunMul_Time();
            #else
			ERROR_STATUS();
            #endif
			
			key_flag = 0;
		}
		else
		{
			char str1[key_len],str2[6];
			num_str(key_buffer,key_len,str1);
			num_str(admin_key->keys,6,str2);
			s = strstr(str1,str2);
			if(s != 0)//是管理密钥
			{
				key_flag = 3;
				key_len = 0;
				printf("change key \r\n");
				SUCCESS_STATUS();
				return;
			}
			num_str(user_key->keys,6,str2);
			s = strstr(str1,str2);
			if(s != 0)
			{
				key_flag = 0;
				#if DEBUG
				printf("unlock\r\n");
				Buzzer_RunMul_Time();
				#else
				SUCCESS_STATUS();
				#endif
			}
			else
			{
				key_flag = 0;
				#if DEBUG
				printf("key error\r\n");
				Buzzer_RunMul_Time();
				#else
				ERROR_STATUS();
				#endif
				
			}
		}
	}
	Buzzer_RunTime(50);
}

//读头模式
void read_uidstatus()
{
	if(cr == '#')
	{
		key_flag = 0;
		char str1[key_len],str2[2] = {'2','6'},str3[2] = {'3','4'};
		num_str(key_buffer,key_len,str1);

		
		
		s = strstr(str1,str2);
		if(s != NULL)//26模式
		{
			wg_flag = 1;
			#if DEBUG
			printf("韦根26\r\n");
			Buzzer_RunMul_Time();
			#else
			SUCCESS_STATUS();
			#endif
		}
		s = strstr(str1,str3);
		if(s != NULL)//34模式
		{
			wg_flag = 2;
			#if DEBUG
			printf("韦根34\r\n");
			Buzzer_RunMul_Time();
			#else
			SUCCESS_STATUS();
			#endif
		}
		
		if(wg_flag == 0)
		{
			ERROR_STATUS();
		}
	}
	else
	{
		key_buffer[key_len++] = cr;
	}
}

void command_select()
{
	if(cr == '*')//退出编程模式
	{
		key_flag = 0;
		return;
	}
	switch(cr)
	{
		case 1://修改管理员密钥
		key_flag = 4;
		break;
				
		case 2://修改用户密钥
		key_flag = 5;
		break;
				
		case 3://修改增加卡的uid
		change_admin_uid_flag = 1;
		key_flag = 0;	
		break;
				
		case 4:	//修改减少卡uid
		change_admin_uid_flag = 2;
		key_flag = 0;
		break;
				
		case 5://删除所有卡
		key_flag = 6;
		break;
				
		case 6://格式化
		key_flag = 7;
		break;
				
		case 7: //读头模式
		key_flag = 8;
		break;
		
	}
}

//格式化卡片 0 删除所有卡 1回复出场设置

//格式化卡片 0 删除所有卡 1回复出场设置
void Format_uf(uint8_t *k_buf,uint8_t flg,key_6byte *admin_key)
{
	if(cr == '*')
	{
		key_flag = 0;
	}
	else
	{
		if(cr == '#')
		{
			
			if(key_len<6)
			{
				#if DEBUG
				printf("admin key error\r\n");
				Buzzer_RunMul_Time();
				#else
				ERROR_STATUS();
				#endif
				key_flag = 0;
				return;
			}
			else
			{
				char str1[key_len],str2[6];
				num_str(key_buffer,key_len,str1);
				num_str(admin_key->keys,6,str2);
				s = strstr(str1,str2);
				if(s!=NULL)
				{
					if(flg == 0)
					{
						format_card();
						printf("format card ok!\r\n");
					}
					else
					{
						Format_moudle();
						fk_flag = 1;
						printf("format flash ok!\r\n");
					}
					SUCCESS_STATUS();
				}
			}
			key_flag = 0;
		}
		else
		{
			key_buffer[key_len++] = cr;
		}
	}
	
}

//0修改管理密钥 1修改用户密钥
void key_b(uint8_t *k_buf,uint8_t flg,key_6byte *admin_key,key_6byte *user_key)
{
	if(cr == '*')//退出编程模式
	{
		key_flag = 0;
		return;
	}
	
	if(cr == '#')//确认键
	{
		if(key_len !=6)
		{
			key_flag = 0;
			#if DEBUG
			printf("The key contains 6 characters\r\n");
			Buzzer_RunMul_Time();
			#else
			ERROR_STATUS();
			#endif
		}
		else
		{
			char *y;
			char str1[key_len],str2[6];
			num_str(key_buffer,key_len,str1);
			num_str(admin_key->keys,6,str2);
			s = strstr(str1,str2);
			num_str(user_key->keys,6,str2);
			y = strstr(str1,str2);
			if(s != NULL||y !=NULL)//与原密钥相同
			{
				printf("key same or admin key The user key is the same as the management key\r\n");
				ERROR_STATUS();
				key_flag = 0;
				return;
			}
			uint8_t i;
			for(i=0;i<6;i++)
			{
				buf_key.keys[i] = key_buffer[i];
			}
			Flash_Changekey(flg,&buf_key);
			printf("change key ok!\r\n");
			fk_flag = 1;
			key_flag = 0;
			SUCCESS_STATUS();
		}
	}
	else
	{
		key_buffer[key_len++] = cr;
	}	
}


/**
 * @brief num_str将0~9的数值转换成ascii的对应字符串
 * @param  : 
 * @param  :
 * @retval 
 */
void num_str(uint8_t *str,uint8_t len,char *x)
{
	uint8_t i;
	for(i=0;i<len;i++)
	{ 
		x[i] = str[i] + 48;
	}
}


